/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.coupon.entity.FxPersonAccount;
import com.ykht.proj.coupon.service.FxPersonAccountService;
import com.ykht.proj.coupon.vo.FxPersonAccountVo;
import com.ykht.proj.coupon.vo.FxWithdrawalPersonVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.security.Security;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * c端账户表
 *
 * @author proj code generator
 * @date 2021-08-12 14:10:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxpersonaccount")
@Api(value = "fxpersonaccount", tags = "c端账户表管理")
@Slf4j
public class FxPersonAccountController {

	private final FxPersonAccountService fxPersonAccountService;

	/**
	 * 分页查询 c端账户表
	 *
	 * @param page              分页对象
	 * @param fxPersonAccountVo c端账户表
	 * @return
	 */
	@ApiOperation(value = "分页查询 c端账户表", notes = "分页查询 c端账户表")
	@GetMapping("/page")
	public R<IPage<FxPersonAccountVo>> getFxPersonAccountPage(Page page, FxPersonAccountVo fxPersonAccountVo) {
		log.info("->分页查询 c端账户表 :{}", page, fxPersonAccountVo);
		IPage<FxPersonAccountVo> iPage = fxPersonAccountService.getFxPersonAccountPage(page, fxPersonAccountVo);
		return R.ok(iPage);
	}

	/**
	 * 分页查询 c端账户提现记录列表
	 *
	 * @param page              分页对象
	 * @param fxPersonAccountVo c端账户表
	 * @return
	 */
	@ApiOperation(value = "分页查询 c端账户提现记录列表", notes = "分页查询 c端账户提现记录列表")
	@GetMapping("/getCWithdrawalRecordPage")
	public R<IPage<FxPersonAccountVo>> getCWithdrawalRecordPage(Page page, FxPersonAccountVo fxPersonAccountVo) {
		log.info("->分页查询 c端账户提现记录列表 :{}", page, fxPersonAccountVo);
		IPage<FxPersonAccountVo> iPage = fxPersonAccountService.getCWithdrawalRecordPage(page, fxPersonAccountVo);
		return R.ok(iPage);
	}


	/**
	 * 通过id查询c端账户表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxpersonaccount_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxPersonAccountService.getById(id));
	}

	/**
	 * 新增c端账户表
	 *
	 * @param fxPersonAccount c端账户表
	 * @return R
	 */
	@ApiOperation(value = "新增c端账户表", notes = "新增c端账户表")
	@SysLog("新增c端账户表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxpersonaccount_add')")
	public R save(@RequestBody FxPersonAccount fxPersonAccount) {
		return R.ok(fxPersonAccountService.save(fxPersonAccount));
	}

	/**
	 * 修改c端账户表
	 *
	 * @param fxPersonAccount c端账户表
	 * @return R
	 */
	@ApiOperation(value = "修改c端账户表", notes = "修改c端账户表")
	@SysLog("修改c端账户表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxpersonaccount_edit')")
	public R updateById(@RequestBody FxPersonAccount fxPersonAccount) {
		return R.ok(fxPersonAccountService.updateById(fxPersonAccount));
	}

	/**
	 * 通过id删除c端账户表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除c端账户表", notes = "通过id删除c端账户表")
	@SysLog("通过id删除c端账户表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxpersonaccount_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(fxPersonAccountService.removeById(id));
	}

	@ApiOperation(value = "导出客户体现的详情表")
	@PostMapping("ExportExcel")
	@Inner(false)
	public void ExportExcel( HttpServletResponse response) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("客户体现的详情表");
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell = row.createCell((short) 1);
		cell.setCellValue("订单号");
		cell = row.createCell((short) 2);
		cell.setCellValue("提现时间");
		cell = row.createCell((short) 3);
		cell.setCellValue("用户名");
		cell = row.createCell((short) 4);
		cell.setCellValue("手机");
		cell = row.createCell((short) 5);
		cell.setCellValue("提现金额");
		cell = row.createCell((short) 6);
		cell.setCellValue("剩余金额");
		cell = row.createCell((short) 7);
		cell.setCellValue("提现方式");
		cell = row.createCell((short) 8);
		cell.setCellValue("微信交易");
		List<FxWithdrawalPersonVo> list = fxPersonAccountService.getExport();
		for (int i = 0; i < list.size(); i++) {
			HSSFRow row1 = sheet.createRow((int) i + 1);
			FxWithdrawalPersonVo fx = list.get(i);
			row1.createCell((short) 0).setCellValue(fx.getId());
			row1.createCell((short) 1).setCellValue(fx.getOrderNumber());
			String currentTime = fx.getSuccessTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
			row1.createCell((short) 2).setCellValue(currentTime);
			row1.createCell((short) 3).setCellValue(fx.getPersonName());
			row1.createCell((short) 4).setCellValue(fx.getPersonPhone());
			row1.createCell((short) 5).setCellValue(fx.getMoney().toString());
			row1.createCell((short) 6).setCellValue(fx.getBalance().toString());
			row1.createCell((short) 7).setCellValue(fx.getWithdrawalType());
			row1.createCell((short) 8).setCellValue(fx.getWxOrder());
		}
		try {
			/*FileOutputStream fileOutputStream = new FileOutputStream("D:\\xiangqing.xlsx");
			wb.write(fileOutputStream);
			fileOutputStream.close();*/
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("tixianbiao.xls", "UTF-8"));
			response.setHeader("Connection", "close");
			response.setHeader("Content-Type", "application/octet-stream");
			wb.write(response.getOutputStream());
			wb.close();
		} catch (Exception e) {
			System.out.println("导出失败");
			e.printStackTrace();
		}

	}

	@ApiOperation(value = "导出客户表")
	@PostMapping("ExportExcelPerson")
	@Inner(false)
	public void ExportExcelPerson( HttpServletResponse response) {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("客户表");
		HSSFRow row = sheet.createRow((int) 0);
		HSSFCell cell = row.createCell((short) 0);
		cell.setCellValue("序号");
		cell = row.createCell((short) 1);
		cell.setCellValue("用户名");
		cell = row.createCell((short) 2);
		cell.setCellValue("手机号");
		cell = row.createCell((short) 3);
		cell.setCellValue("累计提现");
		cell = row.createCell((short) 4);
		cell.setCellValue("可提现金额");
		List<FxPersonAccountVo> list = fxPersonAccountService.ExportExcelPerson();
		for (int i = 0; i < list.size(); i++) {
			HSSFRow row1 = sheet.createRow((int) i + 1);
			FxPersonAccountVo fx = list.get(i);
			row1.createCell((short) 0).setCellValue(fx.getId());
			row1.createCell((short) 1).setCellValue(fx.getPersonName());
			row1.createCell((short) 2).setCellValue(fx.getPersonPhone());
			row1.createCell((short) 3).setCellValue(fx.getWithdrawal().toString());
			row1.createCell((short) 4).setCellValue(fx.getBalance().toString());
		}
		try {
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("kehubiao.xls", "UTF-8"));
			response.setHeader("Connection", "close");
			response.setHeader("Content-Type", "application/octet-stream");
			wb.write(response.getOutputStream());
			wb.close();
		} catch (Exception e) {
			System.out.println("导出失败");
			e.printStackTrace();
		}

	}



	}



